<?php
//开启session
session_start();
//接收主帖id
$tid = $_GET['tid'];
//接收版块id
$cid = $_GET['cid'];
if(empty($_SESSION['username'])){
	echo '<script>alert("请先登录");window.location.href="list.php?cid='.$cid.'";</script>';
	exit;
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>帖子详情</title>
		<link href="css/detail.css" rel="stylesheet" type="text/css">
		<link href="css/public.css" rel="stylesheet" type="text/css">
	</head>
	<body>
		<?php 
			include './includes/header.inc.php';
		?>
		<!--帖子详情-->
		<div id="detail">
			<?php
				//查询发表帖子信息
				$tsql = "select ftiezi.id,ftiezi.uid,user.age,user.username,user.pic,ftiezi.title,.ftiezi.content,.ftiezi.rtime,ftiezi.look,ftiezi.pinbi,ftiezi.fujian,ftiezi.high from user,ftiezi where ftiezi.uid=user.id and ftiezi.id={$tid}";
				//执行sql
				$tres = mysqli_query($link, $tsql);
				//判断执行结果
				if($tres && mysqli_num_rows($tres)){
					$tinfo = mysqli_fetch_assoc($tres);
					//释放结果集
					mysqli_free_result($tres);
				}
				
				if($_SESSION['qx'] != 1){
			?>
				<div id="pinbi">
					<ul>
						<?php
							if($tinfo['pinbi'] == 0){
						?>
							<li><a href="pinbi.php?fid=<?php echo $tid?>&cid=<?php echo $cid?>">屏蔽该贴</a></li>
						<?php	
							}else{
						?>
							<li><a href="jiepin.php?fid=<?php echo $tid?>&cid=<?php echo $cid?>">解屏</a></li>
						<?php
							}
						?>
						<li> | </li>
						<li>置顶</li>
						<li> | </li>
						<?php
							if($tinfo['high'] == 0){
						?>
							<li><a href="./gaoliang/high.php?fid=<?php echo $tid?>&cid=<?php echo $cid?>">高亮</a></li>
						<?php	
							}else{
						?>
							<li><a href="./gaoliang/unhigh.php?fid=<?php echo $tid?>&cid=<?php echo $cid?>">解除高亮</a></li>
						<?php
							}
						?>
						<li> | </li>
						<li>加精</li>
						<li><a href="list.php?tid=<?php echo $tid?>&cid=<?php echo $cid?>">返回列表</a></li>
					</ul>
				</div>
			<?php
				}else{
			?>
					<ul>
						<li><a href="list.php?tid=<?php echo $tid?>&cid=<?php echo $cid?>">返回列表</a></li>
					</ul>
			<?php
				}
			?>
			
			<div class="clear"></div>
			<div class="detail1">
				<?php
					//查询回复数
					$sqlh = "select count(*) from ftiezi,htiezi where htiezi.fid=ftiezi.id and ftiezi.id={$tinfo['id']}";
					//执行sql
					$resh = mysqli_query($link, $sqlh);
					//取值
					$c = mysqli_fetch_row($resh);
				?>
				
				<div class="detail_a">回复:<?php echo $c[0]?>|查看:<?php echo $tinfo['look']?></div>
				<?php
					//插入查询数
					$sqlc = "update ftiezi set look=({$tinfo['look']}+ 1) where id={$tinfo['id']}";
					//执行sql
					$resc = mysqli_query($link, $sqlc);
				?>
				<div class="detail_b"><b><?php echo $tinfo['title']?></b></div>
			</div>
			<div class="detail_1">
				<div class="detail_1_top">
					<div class="detail_1_top_left"><b><?php echo $tinfo['username']?></b></div>
					<div class="detail_1_top_right">发表于:<?php echo date('Y-m-d H:i:s',$tinfo['rtime'])?></div>
				</div>
				<div class="detail_1_footer">
					<div class="detail_1_footer_left">
						<img height="70" width="100" src="../images/<?php echo $tinfo['pic']?>">
						<ul>
							<li>UID:<?php echo $tinfo['uid']?></li>
							<li>NAME:<?php echo $tinfo['username']?></li>
							<li>AGE:<?php echo $tinfo['age']?></li>
						</ul>
						<div style="margin-top:10px;">
						<?php
							//准备sql
							$attsql = "select id,uid,attid from att where uid={$_SESSION['id']} and attid={$tinfo['uid']}";
							//执行sql
							$attres = mysqli_query($link, $attsql);
							//判断执行结果
							if($attres && mysqli_num_rows($attres)){
								$attinfo = mysqli_fetch_assoc($attres);
							//释放结果集
							mysqli_free_result($attres);
							}
							//判断用户是否关注
							if($attinfo['uid'] == $_SESSION['id']){
						?>
							<a href="qxatt.php?uid=<?php echo $tinfo['uid']?>&fid=<?php echo $tid?>&cid=<?php echo $cid?>">已关注</a>
						<?php
							}else{
						?>
							<a href="att.php?uid=<?php echo $tinfo['uid']?>&fid=<?php echo $tid?>&cid=<?php echo $cid?>">加关注</a>
						<?php
							}
						?>
						 | 
						<a href="fri.php?uid=<?php echo $tinfo['uid']?>&fid=<?php echo $tid?>&cid=<?php echo $cid?>">加好友</a></div>
					</div>
					<div class="detail_1_footer_right">
						<p>
							<?php 
								if($tinfo['pinbi'] == 0){
									echo $tinfo['content'].'<br>';
									if($tinfo['fujian'] != ''){
										echo '该贴有附件<a href="xiazai.php?funame='.$tinfo['fujian'].'">点击下载</a>';
									}
								}else{
									if($_SESSION['qx'] != 1){
										echo '提示：该贴已被管理员屏蔽,只有有管理权限的人可见<br>';
										echo $tinfo['content'].'<br>';
									}else{
										echo '提示：该贴已被管理员屏蔽,只有有管理权限的人可见';
									}
								}
							?>
						</p>
					</div>
				</div>
			</div>
			<?php
				//当前页
				$page = isset($_GET['page']) ? $_GET['page'] : 1;
				//每页显示几条
				$pagerows = 3;
				//从哪里开始显示
				$offset = ($page - 1) * $pagerows;
				//拼接limit子句
				$limit = "limit {$offset},{$pagerows}";
				
				//准备查询回复帖子
				$hsql = "select h.id,h.uid,h.fid,h.content,h.rtime,u.username,u.age,u.pic from htiezi h,user u where h.uid=u.id and h.fid={$tid} order by id {$limit}";
				//echo $hsql;
				//exit;
				//执行sql
				$hres = mysqli_query($link, $hsql);
				//判断执行结果
				if($hres && mysqli_num_rows($hres)){
					$a = isset($_GET['a']) ? $_GET['a'] : 1;
					while($hinfo = mysqli_fetch_assoc($hres)){
						
			?>
			<div class="detail_1">
				<div class="detail_1_top">
					<div class="detail_1_top_left"><b><?php echo $hinfo['username']?></b></div>
					<div class="detail_1_top_right">楼主&nbsp;回复于:<?php echo date('Y-m-d H:i:s',$hinfo['rtime'])?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $a?>楼</div>
				</div>
				<div class="detail_1_footer">
					<div class="detail_1_footer_left">
						<img height="70" width="100" src="../images/<?php echo $hinfo['pic']?>">
						<ul>
							<li>UID:<?php echo $hinfo['uid']?></li>
							<li>NAME:<?php echo $hinfo['username']?></li>
							<li>AGE:<?php echo $hinfo['age']?></li>
						</ul>
						<div style="margin-top:10px;">
						<?php
						//判断用户是否关注
							if($attinfo['uid'] == $_SESSION['id']){
						?>
							<a href="qxatt.php?uid=<?php echo $tinfo['uid']?>&fid=<?php echo $tid?>&cid=<?php echo $cid?>">已关注</a>
						<?php
							}else{
						?>
							<a href="att.php?uid=<?php echo $tinfo['uid']?>&fid=<?php echo $tid?>&cid=<?php echo $cid?>">加关注</a>
						<?php
							}
						?> | 
						<a href="fri.php?uid=<?php echo $tinfo['uid']?>&fid=<?php echo $tid?>&cid=<?php echo $cid?>">加好友</a></div>
					</div>
					<div class="detail_1_footer_right">
						<p><?php echo $hinfo['content']?></p>
					</div>
				</div>
			</div>
			<?php
					$a++;
					}
					//释放结果集
					mysqli_free_result($hres);
				}
			?>
			<div class="clear"></div>
			<?php
				//总页数 = ceil(总条数 / 每页显示的条数)
				$cpage = ceil($c[0] / $pagerows);
				//上一页
				$prevpage = $page - 1;
				if($prevpage < 1){
					$prevpage = 1;
				}
				//下一页
				$nextpage = $page + 1;
				if($nextpage > $cpage){
					$nextpage = $cpage;
				}
				
				//上一页楼层的值
				$preva = ($page - 1) * $pagerows + 1;
				if($preva < 1){
					$preva = 1;
				}
				//下一页楼层的值
				$nexta = $page * $pagerows + 1;
				if($nexta > $c[0]){
					$nexta = $c[0] - $pagerows + 1;
				}
				//尾页楼层的值 = 总回复数 - 每页显示的条数
				
				
			?>
			<div>
				<a href="detail.php?tid=<?php echo $tid?>&page=1&a=1">首页</a>
				<a href="detail.php?tid=<?php echo $tid?>&page=<?php echo $prevpage?>&a=<?php echo $preva?>">上一页</a>
				<a href="detail.php?tid=<?php echo $tid?>&page=<?php echo $nextpage?>&a=<?php echo $nexta?>">下一页</a>
				<a href="detail.php?tid=<?php echo $tid?>&page=<?php echo $cpage?>&a=<?php  ?>">尾页</a>
			</div>
			<div class="clear"></div>
			<?php
				//查询用户头像
				$usql = "select pic from user where id={$_SESSION[id]}"; 
				//执行sql
				$ures = mysqli_query($link, $usql);
				//判断执行结果
				if($ures && mysqli_num_rows($ures)){
					$uinfo = mysqli_fetch_assoc($ures);
				}
			?>
			<form method="post" action="dodetail.php">
				<input type="hidden" name="tid" value="<?php echo $tid?>">
				<div id="content">
					<div id="content_left">
						<img src="../images/<?php echo $uinfo['pic']?>">
					</div>
					<div id="content_right">
						<textarea name="content"></textarea>
						验证码：<input type="text" name="yzm" size="4">
						<img src="../images/yzm.jpg"><br><br>
						<input type="submit" value="发表回复">
					</div>
				</div>
			</form>
		</div>
		<div class="clear"></div>
		<!--底部 友链  版权-->
		<?php 
			include './includes/footer.inc.php';
		?>
	</body>
</html>